package com.gr.jron.model;

import javax.persistence.*;
import java.util.*;

/**
 * Created by Jronrun.
 * User: jron
 * Date: 2007-7-21
 * Time: 10:25:18
 * It's jron's dream to cqiu
 */
@Entity
@Table(name = "JCATEGORY")
public class Category extends BaseObject {

    private Long id;

    private int version = 0;

    private String categoryName;
    private Category parentCategory;
    private List<Category> childCategories = new ArrayList<Category>();
    private List<Product> products = new ArrayList<Product>();
    private Set<CategorizedProduct> categorizedProducts = new HashSet<CategorizedProduct>();
//    private Map<Product, User> productsAndUser = new HashMap<Product, User>();
    private Date categoryTime;

    public Category() {}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Version
    public int getVersion() {
        return version;
    }

    @Column(nullable = false)
    public String getCategoryName() {
        return categoryName;
    }

    @OneToMany(mappedBy = "parentCategory",
            cascade = {CascadeType.PERSIST,CascadeType.MERGE}
    )
    public List<Category> getChildCategories() {
        return childCategories;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PARENT_CATEGORY_ID", nullable = true)
    public Category getParentCategory() {
        return parentCategory;
    }

    @ManyToMany
    @JoinTable(
        name = "JCATEGORY_PRODUCT",
        joinColumns =        @JoinColumn(name = "CATEGORY_ID"),
        inverseJoinColumns = @JoinColumn(name = "PRODUCT_ID")
    )
    public List<Product> getProducts() {
        return products;
    }

    @OneToMany(cascade = CascadeType.ALL)
    public Set<CategorizedProduct> getCategorizedProducts() {
        return categorizedProducts;
    }

    public void setCategorizedProducts(Set<CategorizedProduct> categorizedProducts) {
        this.categorizedProducts = categorizedProducts;
    }

    /*
    @ManyToMany
    @JoinTable(
        name = "CATEGORY_PRODUCTS_BY_USER",
        joinColumns = @JoinColumn(name = "CATEGORY_ID"),
        inverseJoinColumns = @JoinColumn(name = "USER_ID")
    )
    public Map<Product, User> getProductsAndUser() {
        return productsAndUser;
    }
*/

    @Column(nullable = false, updatable = false)
    public Date getCategoryTime() {
        return categoryTime;
    }

    public void setCategoryTime(Date categoryTime) {
        this.categoryTime = categoryTime;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public void setChildCategories(List<Category> childCategories) {
        this.childCategories = childCategories;
    }

    public void setParentCategory(Category parentCategory) {
        this.parentCategory = parentCategory;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Category category = (Category) o;

        if (version != category.version) return false;
        if (categoryName != null ? !categoryName.equals(category.categoryName) : category.categoryName != null)
            return false;
        if (categoryTime != null ? !categoryTime.equals(category.categoryTime) : category.categoryTime != null) return false;
        if (id != null ? !id.equals(category.id) : category.id != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + version;
        result = 31 * result + (categoryName != null ? categoryName.hashCode() : 0);
        result = 31 * result + (categoryTime != null ? categoryTime.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "Category{" +
                "id=" + id +
                ", version=" + version +
                ", created=" + categoryTime +
                ", categoryName='" + categoryName + '\'' +
                '}';
    }
}
